package com.supreme.nettyapp.generalclient.mqttclient.handler;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.mqtt.MqttDecoder;
import io.netty.handler.codec.mqtt.MqttEncoder;
import io.netty.handler.codec.mqtt.MqttMessage;

public class MqttClientInitializer extends ChannelInitializer<SocketChannel> {

    private final SimpleChannelInboundHandler<MqttMessage> mqttClientHandler;

    public MqttClientInitializer(SimpleChannelInboundHandler<MqttMessage> mqttClientHandler) {
        this.mqttClientHandler = mqttClientHandler;
    }

    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        ChannelPipeline pipeline = socketChannel.pipeline();
        pipeline.addLast("decoder", new MqttDecoder());
        pipeline.addLast("encoder", MqttEncoder.INSTANCE);
        pipeline.addLast("handler", mqttClientHandler);
    }
}
